package com.yinshan.dialog.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yinshan.dialog.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author limh
 * @function
 * @date 2019/1/28 14:47
 */
public class KeyBoardAdapter extends BaseAdapter {
    private ArrayList<String> keys = new ArrayList();
    private LayoutInflater mInflater;

    public KeyBoardAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.keys.clear();
        String[] str = {"1", "5", "7", "3", "4", "9", "8", "0", "6", "2"};
        List<String> list = Arrays.asList(str);
        List<String> tmp = new ArrayList<>(list);
        Random random = new Random();
        while (keys.size() < 10) {
            int n = random.nextInt(tmp.size());
            keys.add(tmp.get(n));
            tmp.remove(n);
        }
        String nn = keys.get(9);
        keys.set(9, "");
        keys.add(nn);
        keys.add("x");
    }

    public int getCount() {
        return this.keys.size();
    }

    public Object getItem(int position) {
        return this.keys.get(position);
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        KeyBoardAdapter.ViewHolder holder;
        if (null == convertView) {
            convertView = this.mInflater.inflate(R.layout.layout_dialog_item_key, parent, false);
            holder = new KeyBoardAdapter.ViewHolder();
            holder.txtKey = convertView.findViewById(R.id.txt_item_key);
            holder.imgDel = convertView.findViewById(R.id.img_item_del);
            holder.lineBg = convertView.findViewById(R.id.line_bg);
            convertView.setTag(holder);
        } else {
            holder = (KeyBoardAdapter.ViewHolder) convertView.getTag();
        }

        if (!"x".equals(this.keys.get(position))) {
            holder.txtKey.setText(this.keys.get(position));
            holder.imgDel.setVisibility(View.GONE);
            holder.txtKey.setVisibility(View.VISIBLE);
            holder.lineBg.setBackgroundColor(Color.parseColor("#FFFFFF"));
        } else {
            holder.lineBg.setBackgroundColor(Color.parseColor("#CCCCCC"));
            holder.txtKey.setVisibility(View.GONE);
            holder.imgDel.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    public ArrayList<String> getKeys() {
        return this.keys;
    }

    class ViewHolder {
        TextView txtKey;
        ImageView imgDel;
        LinearLayout lineBg;

        ViewHolder() {
        }
    }
}
